﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TouristAgencyDataLayer
{
    public class ToursHotelFacilities
    {
        public static void CreateTourHotelFacility(int tourId, int hf)
        {
            TouristAgencyDataClassesDataContext context = new TouristAgencyDataClassesDataContext();

            ToursHotelFacility m = new ToursHotelFacility()
            {
                HotelFacilityId = hf,
                TourId = tourId
            };

            context.ToursHotelFacilities.InsertOnSubmit(m);
            context.SubmitChanges();
        }

        public static void DeleteTourHotelFacility(int tourId, int hf)
        {
            TouristAgencyDataClassesDataContext context = new TouristAgencyDataClassesDataContext();

            var res =
                (from b in context.ToursHotelFacilities
                 where b.HotelFacilityId == hf && b.TourId == tourId
                 select b).FirstOrDefault();

            if (res != null)
            {
                context.ToursHotelFacilities.DeleteOnSubmit(res);
                context.SubmitChanges();
            }
        }

        public static IQueryable<HotelFacility> GetTourHotelFacolities(int tourId)
        {
            TouristAgencyDataClassesDataContext context = new TouristAgencyDataClassesDataContext();

            var res =
                from thf in context.ToursHotelFacilities
                where thf.TourId == tourId
                join hf in context.HotelFacilities on thf.HotelFacilityId equals hf.HotelFacilityId
                select hf;

            return res;
        }

        public static bool Contains(int tourId, int hfId)
        {
            TouristAgencyDataClassesDataContext context = new TouristAgencyDataClassesDataContext();

            var res =
                (from hf in context.ToursHotelFacilities
                 where hf.TourId == tourId && hf.HotelFacilityId == hfId
                 select hf).FirstOrDefault();

            if (res == null)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        public static void DeleteAllHotelFacilitiesOnTour(int tourId)
        {
            TouristAgencyDataClassesDataContext context = new TouristAgencyDataClassesDataContext();

            var res =
                from thf in context.ToursHotelFacilities
                where thf.TourId == tourId
                select thf;

            foreach (var item in res)
            {
                context.ToursHotelFacilities.DeleteOnSubmit(item);
            }

            context.SubmitChanges();
        }
    }
}
